﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfileTypeActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The profile type actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Profiles
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The profile type actioner.
    /// </summary>
    public class ProfileTypeActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProfileTypeActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public ProfileTypeActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create profile type.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="sector">
        /// The sector.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateProfileType(string name, ProfileSector sector)
        {
            var type = new ProfileType
                           {
                               ID = Guid.NewGuid(),
                               Name = name,
                               Sector = sector,
                               CreatedDateTime = DateTime.Now
                           };
            this.Session.Save(type);

            this.Log.CreateLogEntry(
                type.ID, string.Format("Profile relation '{0}', blev oprettet.", type.Name), Enums.LogEntryType.ProfileType);

            return type.ID;
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileTypesResult"/>.
        /// </returns>
        public ProfileTypesResult ExecuteSearchQuery(ProfileTypesQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new ProfileTypesResult();

            IQueryOver<ProfileType, ProfileType> search = this.Session.QueryOver<ProfileType>();
            this.ApplyDefaultCriterias(settings, search);
            
            result.TotalRowCount = search.RowCount();
            var types = this.ApplySkipAndTake(settings, search.OrderBy(x => x.Name).Asc);

            foreach (ProfileType profileType in types)
            {
                Services.Dto.ProfileType dto = Services.Dto.ProfileType.Convert(profileType);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get profile type.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileType"/>.
        /// </returns>
        public ProfileType GetProfileType(Guid id)
        {
            var type = this.Session.Get<ProfileType>(id);

            return type;
        }

        #endregion
    }
}